package net.hn.hnms.biz.safety.water.group;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.api.safety.support.domain.MineNowAlarmVo;
import net.hn.hnms.biz.safety.water.company.domain.CGKAlarmVO;
import net.hn.hnms.biz.safety.water.company.service.CompanyService;
import net.hn.hnms.biz.safety.water.domain.HolealAbnormalPo;
import net.hn.hnms.biz.safety.water.mapper.HolealAbnormalMapper;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Author: PanSX
 * Date: 2023/4/7 14:07
 */
@RestController
@RequestMapping("group/riskMonitor/")
@Api(value = "水害集团端接口", tags = {"水害-集团端"})
public class GroupController {
    @Resource
    private HolealAbnormalMapper holealAbnormalMapper;

    /**
     * 公司端接口
     */
    @Resource
    private CompanyService companyService;
    public List<MineNowAlarmVo> nowAlarm(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String str = format.format(date);
        String startTime = str + " 00:00:00";
        String endTime = str + " 23:59:59";
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = formatDate.parse(startTime);
            endDate = formatDate.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        QueryWrapper<HolealAbnormalPo> wrapper = new QueryWrapper<HolealAbnormalPo>();
        wrapper.between("data_time",startDate,endDate)
        .select(" mine_code,count(1) as count ").groupBy("mine_code");

        List<Map<String, Object>> maps = holealAbnormalMapper.selectMaps(wrapper);
        List<MineNowAlarmVo> list=new ArrayList<>();
        for (Map<String, Object> map:maps){
            MineNowAlarmVo vo=new MineNowAlarmVo();
            vo.setMineCode(map.get("mine_code")+"");
            vo.setCount(Integer.parseInt(map.get("count")+""));
            list.add(vo);
        }
        return list;
    }
    @GetMapping("/getTodayCGKAlarmList")
    @ApiOperation("获取当天长观孔报警列表")
    @AutoSetMineCode
    public R<List<CGKAlarmVO>> getTodayCGKAlarmList(){
        //获取当前公司的所有煤矿
        SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + 0);
        if (sysOrganizationVo == null) {
            throw new HuaNengException(Constants.FAIL, "无法获集团的子煤矿信息，请检查词典");
        }
        List<CGKAlarmVO> voList = new ArrayList<>();
        for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
            //遍历集团所有公司
            List<CGKAlarmVO>  list= companyService.getTodayCGKAlarmList(organizationVo.getOrgCode());
            for (CGKAlarmVO cgkAlarmVO : list) {
                voList.add(cgkAlarmVO);
            }
        }
        return R.ok(voList);

    }
}
